package net.oov.identify.model;

public class MutualInfoModel extends CheckModelAdapter {

	private Double minMi;
	private Integer numWord;
	private FreqCheck fc;
	
	private Double valueBuffered;
	
	public MutualInfoModel(FreqCheck fc, Double minMi, Integer numWord) {
		super();
		this.fc = fc;
		this.minMi = minMi;
		this.numWord = numWord;
	}
	
	/**
	 * 互信息计算
	 * mi = log2 P(AB)/P(A)*P(B)
	 * mi = log2 P(ABC)/p(AB)*P(BC)
	 */
	public Double value(StatusNode ct, CharSequence cs) {
		int numAB = ct.freq();
		int numA = fc.freq(cs.subSequence(0,cs.length()-1));
		int numB = fc.freq(cs.subSequence(1,cs.length()));
//		if(numA<numAB||numB<numAB)return minMi;
		
		valueBuffered = Math.log(((double)numAB*numWord)/(numA*numB));
		return valueBuffered;
	}
	
	/**
	 * 互信息计算
	 * mi = log2 P(AB)/P(A)*P(B)
	 */
	public Double value(StatusNode ct, CharSequence cs1,CharSequence cs2) {
		int numAB = ct.freq();
		int numA = fc.freq(cs1);
		int numB = fc.freq(cs2);
		
		return Math.log(((double)numAB*numWord)/(numA*numB));
	}

	public Boolean check(StatusNode ct, CharSequence cs ) {
		
		Double v = value(ct,cs);
		Boolean ret = minMi <= v;
//		if(!ret)wl(cs + ",M:"+v);
		return ret;
		
	}
	
	public Double value(){
		return valueBuffered;
	}
	
	public static void wl(Object s){
		System.out.println(s);
	}

	public String info(StatusNode ct, CharSequence cs) {
		// TODO Auto-generated method stub
		return null;
	}
	
	
}
